from datetime import datetime

from models import db


class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)  # 用户id
    mobile = db.Column(db.String(11), nullable=False)  # 登录电话号码
    password = db.Column(db.String(128), nullable=False)  # 密码
    nick_name = db.Column(db.String(128), nullable=False)  # 昵称
    creation_time = db.Column(db.DateTime, default=datetime.now())  # 创建时间
    img_url = db.Column(db.String(256), nullable=False)  # 头像
    signature = db.Column(db.String(512), nullable=True, default='这个家伙很懒什么都没留下！')  # 签名
    is_admin = db.Column(db.Enum('0', '1'), default='0')  # 管理员
    gender = db.Column(db.Enum(
        '保密',
        '男',  # 男
        '女'  # 女
    ), default='保密')  # 性别
    birthday = db.Column(db.String(11), nullable=True)  # 生日
    e_mail = db.Column(db.String(64), nullable=True)  # 邮箱
    QQ = db.Column(db.String(32), nullable=True)  # QQ
    mobile_num = db.Column(db.String(11), nullable=False)  # 联系电话

    order = db.relationship('Order_form', lazy='dynamic')  # 关联订单

    shoping_car = db.relationship('Shoping_car', lazy='dynamic')  # 关联购物车


class Commodity(db.Model):
    __tablename__ = 'commodity'

    id = db.Column(db.Integer, primary_key=True)  # 商品id
    name = db.Column(db.String(256), nullable=False)  # 商品名字
    price = db.Column(db.String(128), nullable=False)  # 价格
    img_url = db.Column(db.String(128), nullable=False)  # 商品图片路径
    updata_time = db.Column(db.DateTime, default=datetime.now(), onupdate=datetime.now())  # 更新时间
    outline = db.Column(db.String(512), nullable=False)  # 简介
    sales = db.Column(db.Integer, default=0, nullable=False)  # 销量
    discount = db.Column(db.Float, nullable=False, default=1)  # 折扣
    introduce_img_url = db.Column(db.String(256), nullable=False)  # 商品介绍图片url
    production_num = db.Column(db.String(128), nullable=True)  # 生产许可编号
    edible_time = db.Column(db.String(128), nullable=True)  # 有效期
    packaging_type = db.Column(db.String(128), nullable=True)  # 包装类型
    level = db.Column(db.String(128), nullable=True)  # 级别
    standard_num = db.Column(db.String(128), nullable=True)  # 产品标准号
    production_address = db.Column(db.String(128), nullable=True)  # 产地
    production_time = db.Column(db.DateTime, nullable=True)  # 生产日期
    weight = db.Column(db.String(128), nullable=True)  # 净重量
    technology_type = db.Column(db.String(256), nullable=True)  # 食品工艺
    recommend_num = db.Column(db.String(1), nullable=False, default=1)  # 推荐值
    # brand_id = db.Column(db.Integer, db.ForeignKey('brand_form.id'), nullable=False)  # 品牌id
    classify_id = db.Column(db.Integer, db.ForeignKey('commodity_classify_three.id'))  # 三级分类
    shoping_car = db.relationship('Shoping_car', backref='commodity', lazy='dynamic')  # 关联购物车
    order = db.relationship('Order_form', backref='commodity', lazy='dynamic')  # 关联订单


# 订单表
class Order_form(db.Model):
    __tablename__ = 'order_form'

    id = db.Column(db.Integer, primary_key=True)
    # 将时间和用户id和用户的购物次数合并为订单号
    production_num = db.Column(db.String(1024), primary_key=True, nullable=False)  # 订单号
    commodity_id = db.Column(db.Integer, db.ForeignKey('commodity.id'), nullable=False)  # 商品id
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)  # 用户id
    # store_id = db.Column(db.Integer, db.ForeignKey('store.id'), nullable=False)  # 店铺id
    shoping_num = db.Column(db.String(3), nullable=False)  # 商品数量
    price_count = db.Column(db.String(12), nullable=False)  # 总价
    order_time = db.Column(db.DateTime, default=datetime.now())  # 下单时间


# 订单总价表
# class Order_price_count(db.Model):
#     __tablename__ = 'order_price_count'
#
#     id = db.Column(db.Integer, primary_key=True)
#     order_id = db.Column(db.Integer, db.ForeignKey('commodity.id'), nullable=False)

# # 店铺表
# class Store(db.Model):
#     __tablename__ = 'store'
#
#     id = db.Column(db.Integer, primary_key=True)  # 店铺id
#     name = db.Column(db.String(128), nullable=False)  # 店铺名字
#     order_count = db.relationship('Order_form', lazy='dynamic')  # 店铺订单
#
#
# 购物车
class Shoping_car(db.Model):
    __tablename__ = 'shoping_car'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)  # 用户id
    commodity_id = db.Column(db.Integer, db.ForeignKey('commodity.id'), nullable=False)  # 商品id
    shoping_num = db.Column(db.Integer, nullable=False, default='1')  # 购买数量


# 一级分类
class Commodity_classify_one(db.Model):
    __tablename_ = 'commodity_classify_one'

    id = db.Column(db.Integer, primary_key=True)  # 一级分类id
    name = db.Column(db.String(64), nullable=False)  # 分类名字
    subordinate = db.relationship('Commodity_classify_two', lazy='dynamic')  # 关联二级分类


# 二级分类
class Commodity_classify_two(db.Model):
    __tablename__ = 'commodity_classify_two'

    id = db.Column(db.Integer, primary_key=True)  # 二级分类id
    name = db.Column(db.String(64), nullable=False)  # 分类名字
    classify_one_id = db.Column(db.Integer, db.ForeignKey('commodity_classify_one.id'), nullable=False)  # 一级分类id
    subordinate = db.relationship('Commodity_classify_three', lazy='dynamic')  # 关联三级分类


# 三级分类
class Commodity_classify_three(db.Model):
    __tablename__ = 'commodity_classify_three'

    id = db.Column(db.Integer, primary_key=True)  # 三级分类id
    name = db.Column(db.String(64), nullable=False)  # 分类名字
    classify_two_id = db.Column(db.Integer, db.ForeignKey('commodity_classify_two.id'), nullable=False)  # 二级分类id
    commodity_count = db.relationship('Commodity', lazy='dynamic')  # 三级分类的商品

# # 品牌表
# class Brand_form(db.Model):
#     __tablename__ = 'brand_form'
#
#     id = db.Column(db.Integer, primary_key=True)  # 品牌id
#     name = db.Column(db.String(64), nullable=False)  # 品牌名字
#     brand_commodity = db.relationship('Commodity', backref='role', lazy='dynamic')  # 品牌商品
